import { _decorator, Component, Node } from 'cc';
import { AnimType, BrickCleanAnim, BrickFlyAnim } from '../../../utils/Types';
import { tween } from 'cc';
import Design from '../../../utils/Design';
import BezierUtil from '../../../common/src/util/BezierUtil';
import { Vec3 } from 'cc';
import { Vec2 } from 'cc';
import { Quat } from 'cc';
import { EVENT_NAMES } from '../../../utils/Enums';
import { AudioManager } from '../../../common/src/manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('AnimCtrl')
export class AnimCtrl extends Component {
    // 动画列表
    private animList = []
    // 是否动画中的标记
    private isRuningAnim = false

    // 六边形翻转的方向
    private directions: Vec3[] = [
        new Vec3(Math.sqrt(3)/2, 0, 0.5),     // 右上到左下
        new Vec3(0, 0, 1),                    // 上到下
        new Vec3(-Math.sqrt(3)/2, 0, 0.5),    // 左上到右下
        new Vec3(-Math.sqrt(3)/2, 0, -0.5),   // 左下到右上
        new Vec3(0, 0, -1),                   // 下到上
        new Vec3(Math.sqrt(3)/2, 0, -0.5),    // 右下到左上
    ];

    update(deltaTime: number) {
        if (this.isRuningAnim == false) {
            if (this.animList.length > 0) {
                const anim = this.animList.splice(0, 1)[0]
                switch (anim.type) {
                    case AnimType.BRICKFLY: {
                        this.brickFlyAnim(anim)
                    }
                    break

                    case AnimType.BRICKCLEAN: {
                        this.brickCleanAnim(anim)
                    }
                    break
                }
            }
        }
    }

    // --------------------- 内部方法 --------------------- //
    // 块飞行
    private brickFlyAnim(anim: BrickFlyAnim) {
        this.isRuningAnim = true
        const delayTime = (anim.infos.length - 1) * Design.brickConfig.brickFlyIntervalDuration + Design.brickConfig.brickFlyDuration + Design.brickConfig.towerIntervalDuration

        if (anim.infos.length > 9) {
            AudioManager.playSound("audio:common/翻牌消除音效/翻牌消除x9");
        } else {
            AudioManager.playSound("audio:common/翻牌消除音效/翻牌消除x" + anim.infos.length);
        }

        tween(this.node)
        .call(() => {
            for (var i = 0; i < anim.infos.length; i++) {
                const info = anim.infos[i]
                const controlPoint = new Vec3((info.brick.position.x + info.end.x) / 2, 
                (info.end.y > info.brick.position.y) ? info.end.y + 2 : info.brick.position.y + 2, 
                (info.brick.position.z + info.end.z) / 2)

                tween(info.brick)
                .delay(i * Design.brickConfig.brickFlyIntervalDuration)
                .call(() => {
                    BezierUtil.playBezierTween(info.brick, Design.brickConfig.brickFlyDuration, info.end, controlPoint, "quadInOut")

                    // 计算旋转轴
                    const rotationAxis = this.directions[anim.dir].normalize();
                    // 创建一个四元数，表示旋转 180 度
                    const rotationQuat = new Quat();
                    Quat.rotateAround(rotationQuat, Quat.IDENTITY, rotationAxis, Math.PI);

                    // 创建并执行 tween 动画，翻转动画时间为 1 秒
                    tween(info.brick)
                    .to(Design.brickConfig.brickFlyDuration, { rotation: rotationQuat }, { easing: 'quadInOut' })
                    .call(() => {
                        const rotationQuat = new Quat();
                        Quat.rotateAround(rotationQuat, Quat.IDENTITY, Vec3.ZERO, Math.PI);
                        info.brick.rotation = rotationQuat
                    })
                    .start();

                })
                .start()
            }
        })
        .delay(delayTime)
        .call(() => {
            gg.systemEvent.emit(EVENT_NAMES.BRICK_FLY_END, new Vec2(anim.originX, anim.originZ), new Vec2(anim.endX, anim.endZ))
            this.isRuningAnim = false
        })
        .start()
    }

    // 块清理，缩小到消失
    private brickCleanAnim(anim: BrickCleanAnim) {
        this.isRuningAnim = true
        const delayTime = Design.brickConfig.brickCleanIntervalDuration * (anim.bricks.length - 1) + Design.brickConfig.brickCleanDuration + Design.brickConfig.brickScaleWaitingDuration

        AudioManager.playSound("audio:common/合成音效");
        tween(this.node)
        .call(() => {
            for (var i = 0; i < anim.bricks.length; i++) {
                const brickNode = anim.bricks[i]

                tween(brickNode)
                .delay(Design.brickConfig.brickCleanIntervalDuration * i)
                .to(Design.brickConfig.brickCleanDuration, {scale: new Vec3(0, 1, 0)})
                .removeSelf()
                .start()
            }
        })
        .delay(delayTime + 1)
        .call(() => {
            gg.systemEvent.emit(EVENT_NAMES.BRICK_CLEAN_END, anim)
            this.isRuningAnim = false
            console.log ("消失")
        })
        .start()
    }

    // --------------------- 外部方法 --------------------- //
    public insertAnim(anim) {
        this.animList.push( anim )
    }
}


